package zaphod.toy.gef.japanexample.editparts;

import java.beans.PropertyChangeEvent;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.CompoundBorder;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;

import zaphod.toy.gef.japanexample.model.EditPartWithListener;
import zaphod.toy.gef.japanexample.model.HelloModel;


public class HelloEditPart extends EditPartWithListener
{

    @Override
    protected IFigure createFigure()
    {
        Object model = getModel();
        if ( model == null )
            throw new RuntimeException( "Model is null." );

        if ( model instanceof HelloModel )
        {
            HelloModel newModel = (HelloModel)model;
            Label label = new Label();
            label.setText( newModel.getText() );

            label.setBorder( new CompoundBorder( new LineBorder(),
                    new MarginBorder( 3 ) ) );
            label.setBackgroundColor( ColorConstants.orange );
            label.setOpaque( true );

            return label;
        }

        throw new RuntimeException( "No corresspodning Model : "
                + model.getClass().getName() );
    }


    @Override
    protected void createEditPolicies()
    {
        // TODO Auto-generated method stub

    }


    @Override
    protected void refreshVisuals()
    {
        Object model = getModel();
        if ( model instanceof HelloModel )
        {
            HelloModel castedModel = (HelloModel)model;
            Rectangle constraint = castedModel.getConstraint();

            ((GraphicalEditPart)getParent()).setLayoutConstraint( this,
                    getFigure(), constraint );

            return;
        }

    }


    public void propertyChange( PropertyChangeEvent evt )
    {
        if ( HelloModel.P_CONSTANT.equals( evt.getPropertyName() ) )
        {
            refreshVisuals();
        }

    }
}
